package admin.shell.commands;

import admin.exceptions.*;
import admin.ui.ProxyMediator;

public class EditCommand extends ShellCommand {

	@Override
	public void execute(ProxyMediator proxy, String[] parameters) {
		if (parameters.length < 4) {
			System.out.println("Usage: edit <login> [-p <password>] [-r <role>]");
			return;
		}
		
		String login = parameters[1];
		String newPassword = "";
		String newRole = null;
		
		if (parameters[2].equals("-p")) {
			newPassword = parameters[3];
			
			if (parameters.length >= 6 && parameters[4].equals("-r"))
				newRole = parameters[5];
		}
		
		if (parameters[2].equals("-r")) {
			newRole = parameters[3];
			
			if (parameters.length >= 6 && parameters[4].equals("-p"))
				newPassword = parameters[5];
		}
		
			
		try {
			proxy.editAccount(login, newPassword, newRole);
			System.out.println("Account edited.");
		} catch (InvalidRoleException e) {
			System.out.println("Error: Account edition failed: Invalid role - " + newRole);
		} catch (UnknownUserException e) {
			System.out.println("Error: Account edition failed: User doesn't exist");
		} catch (InsufficientPermissionsException e) {
			System.out.println("Error: Account edition failed: Insufficient permissions to edit this account");
		}
	}
}
